package com.leagueautoattack.util;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.awt.image.BufferedImage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OCRUtil {

    static ITesseract instance = new Tesseract();

    static {

        instance.setDatapath("tessdata");
        //chi_sim ：简体中文， eng：英文    根据需求选择语言库
        instance.setLanguage("chi_sim");

    }

    public static float getAttackSpeed(BufferedImage img) throws TesseractException {

        String result = null;

        result = instance.doOCR(img);

        result = parseNum(result);

        System.out.println(result);

        if (result.equals("")) {

            return 0;

        }

        return Float.parseFloat(result.split(",")[0]);

    }

    public static String parseNum(String str) {

        StringBuilder builder=new StringBuilder();
        String regEx="(\\d+(\\.\\d+)?)";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        while (m.find()) {
            builder.append(m.group()).append(",");
        }
        return builder.toString();
    }
}
